<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of MY_Model.php
 *
 * @author luubk
 * @since Feb 16, 2012 - 3:15:56 PM
 */
class MY_Model extends CI_Model
{

    protected $table;
    protected $prefix = '';
    protected $id = 'id';

    function __construct()
    {
        parent::__construct();
        $this->table = $this->prefix . $this->table;
    }

    protected function _join()
    {
        
    }

    function select_one($where = array())
    {        
        $this->db->select();
        $this->db->from($this->table);       
        $this->_join();

        if (!empty($where))
            foreach ($where as $key => $val)
                $this->db->where($key, $val);

        $query = $this->db->get();

        if ($query->num_rows() > 0)
            return $query->row_array();
        else
            return FALSE;
    }

    function multi_select($where = array(), $order = FALSE, $limit = FALSE, $offset = 0)
    {
        $this->db->select();
        $this->db->from($this->table);

        $this->_join();

        foreach ($where as $key => $val)
            $this->db->where($key, $val);

        if ($order)
            $this->db->order_by($order);

        if ($limit)
            $this->db->limit($limit, $offset);

        $query = $this->db->get();

        if ($query->num_rows() > 0)
            return $query->result_array();
        else
            return FALSE;
    }

    function search($search = array(), $where = array(), $order = FALSE, $limit = FALSE, $offset = 0)
    {
        foreach ($search as $key => $val)
            $this->db->like($key, $val);
        return $this->multi_select($where, $order, $limit, $offset);
    }

    function insert($data)
    {
        if (!is_array($data))
            return FALSE;

        if ($this->db->insert($this->table, $data))
            return $this->db->insert_id();
        else
            return FALSE;
    }

    function update($where = array(), $data)
    {
        if (is_array($where) && count($where) > 0)
        {
            foreach ($where as $key => $val)
                $this->db->where($key, $val);

            if ($this->db->update($this->table, $data))
                return True;
        }
        return FALSE;
    }

    function delete($where = array())
    {
        if (is_array($where) && count($where) > 0)
        {
            foreach ($where as $key => $val)
                $this->db->where($key, $val);

            if ($this->db->delete($this->table))
                return TRUE;
        }
        return FALSE;
    }

}

/** 
 * End file MY_Model.php
 */
